﻿/*======================================================================
	TASK 12:                               { Operators and Expressions}
	We are given integer number n, value v (v=0 or 1) and a position p.
    Write a sequence of operators that modifies n to hold the value v 
    at the position p from the binary representation of n.
	Example: n = 5 (00000101), p=3, v=1 => 13 (00001101)
	n = 5 (00000101), p=2, v=0 => 1 (00000001) 
=======================================================================*/

using System;

class ExchangeBitAtPositionP
{
    static void Main()
    {
        Console.Write("Enter number n=");
        int n = int.Parse(Console.ReadLine());

        Console.Write("Enter bit to exchange v=");
        int v = int.Parse(Console.ReadLine());

        Console.Write("Enter position you want to exchange p=");
        int p = int.Parse(Console.ReadLine());

        Console.WriteLine("n in binary is:{0}", Convert.ToString(n, 2).PadLeft(8, '0'));

        int mask = 1 << p;
        int maskAndn = n & mask;
        int bit = maskAndn >> p;
        Console.WriteLine("Bit in position p is:{0}",bit);
        if (bit==v)
        {
            Console.WriteLine("In this position v = bit that you want to exchange");
        }

        else
        {
            int bitOneToZero = mask ^ n;
            Console.WriteLine("n after exchange is:{0} ,in binary is:{1}", bitOneToZero, Convert.ToString(bitOneToZero, 2).PadLeft(8, '0'));
        }
    }
}

